package com.park.platform.service.impl;

import com.park.common.exception.MessageException;
import com.park.common.poo.BannerInfo;
import com.park.common.poo.PersonalityInfo;
import com.park.common.util.DateUtil;
import com.park.common.util.FileUtil;
import com.park.common.util.StrUtil;
import com.park.platform.dao.IBaseDao;
import com.park.platform.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by leida on 2017/5/10.
 */
@Service
public class BannerServiceImpl extends BaseService implements IBannerService {

    @Autowired
    private IBaseDao baseDao;

    @Override
    public BannerInfo getBannerInfo(int bannerId){
        return baseDao.get(BannerInfo.class, bannerId);
    }

    @Override
    public Map<String, Object> getBanners(){
        Map<String, Object> resultMap = new HashMap<>();
        List<BannerInfo> bannerInfos = baseDao.queryByHql("FROM BannerInfo ORDER BY bannerSeq DESC, updateTime DESC, createTime DESC");
        for(BannerInfo bannerInfo : bannerInfos){
            bannerInfo.setBannerImg(FileUtil.getImgHttp(bannerInfo.getBannerImg()));
        }
        resultMap.put("bannerInfos", bannerInfos);
        return resultMap;
    }

    @Override
    public Integer saveBanner(BannerInfo bannerInfo, MultipartHttpServletRequest multipartRequest) throws IOException{
        Integer bannerId = null;
        String nowDate = DateUtil.getNowDate();
        boolean newFlag = bannerInfo.getBannerId() == null;
        if(!newFlag){
            BannerInfo bannerInfoDB = getBannerInfo(bannerInfo.getBannerId());
            bannerInfoDB.setBannerSeq(bannerInfo.getBannerSeq());
            bannerInfoDB.setBannerUrl(StrUtil.trimToEmpty(bannerInfo.getBannerUrl()));
            bannerInfoDB.setUpdateTime(nowDate);
            baseDao.save(bannerInfoDB, bannerInfoDB.getBannerId());
            bannerId = bannerInfoDB.getBannerId();
        }else{
            bannerInfo.setCreateTime(nowDate);
            baseDao.save(bannerInfo, null);
            bannerId = bannerInfo.getBannerId();
        }
        List<MultipartFile> multipartFiles = FileUtil.getMultipartFiles(multipartRequest);
        if(multipartFiles.size() > 0){ //保存banner图片
            BannerInfo bannerInfoDB = getBannerInfo(bannerInfo.getBannerId());
            bannerInfoDB.setBannerImg(FileUtil.saveFile(multipartFiles.get(0), multipartRequest).toString());
            baseDao.save(bannerInfoDB, bannerInfoDB.getBannerId());
        }else if(newFlag){
            throw new MessageException("请上传banner图片");
        }
        return bannerId;
    }

    @Override
    public void newDeleteBanner(int bannerId){
        BannerInfo bannerInfo = getBannerInfo(bannerId);
        if(bannerInfo != null){
            baseDao.delete(bannerInfo);
        }
    }


    @Override
    public PersonalityInfo getPersonalityInfo(int personalityId){
        return baseDao.get(PersonalityInfo.class, personalityId);
    }

    @Override
    public Map<String, Object> getPersonalitys(){
        Map<String, Object> resultMap = new HashMap<>();
        List<PersonalityInfo> personalityInfos = baseDao.queryByHql("FROM PersonalityInfo ORDER BY personalitySeq DESC, updateTime DESC, createTime DESC");
        for(PersonalityInfo personalityInfo : personalityInfos){
            personalityInfo.setPersonalityImg(FileUtil.getImgHttp(personalityInfo.getPersonalityImg()));
        }
        resultMap.put("personalityInfos", personalityInfos);
        return resultMap;
    }

    @Override
    public Integer savePersonality(PersonalityInfo personalityInfo, MultipartHttpServletRequest multipartRequest) throws IOException{
        Integer personalityId = null;
        String nowDate = DateUtil.getNowDate();
        boolean newFlag = personalityInfo.getPersonalityId() == null;
        if(!newFlag){
            PersonalityInfo personalityInfoDB = getPersonalityInfo(personalityInfo.getPersonalityId());
            personalityInfoDB.setPersonalityTitle(personalityInfo.getPersonalityTitle());
            personalityInfoDB.setPersonalitySeq(personalityInfo.getPersonalitySeq());
            personalityInfoDB.setPersonalityUrl(StrUtil.trimToEmpty(personalityInfo.getPersonalityUrl()));
            personalityInfoDB.setUpdateTime(nowDate);
            baseDao.save(personalityInfoDB, personalityInfoDB.getPersonalityId());
            personalityId = personalityInfoDB.getPersonalityId();
        }else{
            personalityInfo.setCreateTime(nowDate);
            baseDao.save(personalityInfo, null);
            personalityId = personalityInfo.getPersonalityId();
        }
        List<MultipartFile> multipartFiles = FileUtil.getMultipartFiles(multipartRequest);
        if(multipartFiles.size() > 0){ //保存banner图片
            PersonalityInfo personalityInfoDB = getPersonalityInfo(personalityInfo.getPersonalityId());
            personalityInfoDB.setPersonalityImg(FileUtil.saveFile(multipartFiles.get(0), multipartRequest).toString());
            baseDao.save(personalityInfoDB, personalityInfoDB.getPersonalityId());
        }else if(newFlag){
            throw new MessageException("请上传个性推荐图片");
        }
        return personalityId;
    }

    @Override
    public void newDeletePersonality(int personalityId){
        PersonalityInfo personalityInfo = getPersonalityInfo(personalityId);
        if(personalityInfo != null){
            baseDao.delete(personalityInfo);
        }
    }

}
